package com.parkclouds.portal.config.interceptor;

import javax.servlet.http.HttpServletRequest;


import com.parkclouds.api.commom.base.ErrorType;
import com.parkclouds.api.commom.base.Result;
import com.parkclouds.api.commom.exception.AppException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * Copyright: Copyright (c) 2019  zteits
 *
 * @ClassName: com.wx.miniapp.config.interceptor
 * @Description:
 * @version: v1.0.0
 * @author: atao
 * @date: 2019-03-29   11:05
 * Modification History:
 * Date         Author          Version      Description
 * ---------------------------------------------------------*
 * 2019-03-29      atao          v1.0.0          创建
 */
@ControllerAdvice

@Controller
public class WebExceptionHandle {

    private static Logger logger = LoggerFactory.getLogger(WebExceptionHandle.class);

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public ResponseEntity<Result<?>> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        logger.error("参数解析失败", e);
        return new ResponseEntity<>(new Result(ErrorType.PARAM_NOT_VALID, "参数异常"),HttpStatus.OK);
    }
    //
    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public  ResponseEntity<Result<?>> handleHttpRequestMethodNotSupportedException(
        HttpServletRequest request, HttpRequestMethodNotSupportedException e) {
        logger.error("当前请求路径为:"+request.getRequestURI());
        logger.error("不支持当前请求方法", e);
        return new ResponseEntity<>(new Result(ErrorType.AUTH_TOKEN_AUTH_FAIL),HttpStatus.OK);
    }

    /**
     * 404 - Unsupported Media Type
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseBody
    public  ResponseEntity<Result<?>> handleHttpNotFoundException(Exception e) {
        logger.error("请求路径不对", e);
        return new ResponseEntity<>(new Result(ErrorType.URL_NOT_FOUND),HttpStatus.OK);
    }

    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public  ResponseEntity<Result<?>> handleException(Exception e) {
        logger.error("服务运行异常", e);
        if (e instanceof AppException) {
            AppException appException = (AppException)e;
            return new ResponseEntity<>(new Result(appException.getErrCode(), appException.getMessage()),HttpStatus.OK);
        }
        return new ResponseEntity<>(new Result(ErrorType.APP_ERROR, "系统异常"),HttpStatus.OK);
    }
}
